/**
 * 项目名：admin
 * 包名：com.xuebang.admin.service
 * 文件名：ColumnService.java
 * 版本信息：@version 1.0
 * 日期：2014-1-19-上午10:42:45
 */
package com.xuebang.admin.service;

import java.util.List;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.sun.star.uno.RuntimeException;
import com.xuebang.admin.dao.Finder;
import com.xuebang.admin.dao.Finder.Query;
import com.xuebang.admin.entity.MenuColumn;
import com.xuebang.admin.entity.Operator;
import com.xuebang.admin.entity.OperatorGroups;
import com.xuebang.admin.mapper.ColumnMapper;

/**
 * 项目名称：admin
 * 类名称：ColumnService
 * 类描述：
 * 创建人：xuyang
 * 创建时间：2014-1-19 上午10:42:45
 * 修改人：xuyang
 * 修改时间：2014-1-19 上午10:42:45
 * 修改备注：
 * @version 
 */
@Service
public class ColumnService {

	@Inject
	Finder finder;
	
	@Inject
	ColumnMapper columnMapper;
	
	/**
	 * 加载指定id的栏目
	 * 
	 * @param intCid 栏目id
	 */
	@Transactional(readOnly = true)
	public MenuColumn loadColumn(int intCid,boolean checkColumn){
		
		if(checkColumn) {
			// 栏目关联判断
			String strRelatedColumns = getRelatedColumns();
			if(!"".equals(strRelatedColumns)){
				strRelatedColumns = "," + strRelatedColumns + ",";
				if(strRelatedColumns.contains("," + intCid +",")) {
					return columnMapper.loadColumn(intCid);
				} else {
					return null;
				}
			} else {
				return null;
			}
		} else {
			return columnMapper.loadColumn(intCid);
		}
	}
	
	/**
	 * 加载指定id的栏目
	 * 
	 * @param intCid 栏目id
	 */
	@Transactional(readOnly = true)
	public List<MenuColumn> loadFstColumns(boolean checkColumn){
		
		if(checkColumn){
			// 栏目关联判断
			String strRelatedColumns = getRelatedColumns();
			if(!"".equals(strRelatedColumns)){
				return columnMapper.loadCheckedFstColumns(strRelatedColumns.split(","));
			} else {
				return null;
			}
		} else {
			return columnMapper.loadFstColumns();
		}
	}

	/**
	 * 加载指定栏目的子栏目
	 */
	@Transactional(readOnly = true)
	public List<MenuColumn> loadChildColumns(int intCid,boolean checkColumn){
		
		if(checkColumn) {
			// 栏目关联判断
			String strRelatedColumns = getRelatedColumns();
			if(!"".equals(strRelatedColumns)){
				strRelatedColumns = "," + strRelatedColumns + ",";
				if(strRelatedColumns.contains("," + intCid +",")) {
					return columnMapper.loadCheckedChildColumns(intCid,strRelatedColumns.split(","));
				} else {
					return null;
				}
			} else {
				return null;
			}
		} else {
			return columnMapper.loadChildColumns(intCid);
		}
	}
	
	/**
	 * 通过栏目名字查找栏目
	 * 
	 * @param String
	 * 			strMenuColumnName 栏目名字
	 */
	@Transactional(readOnly = true)
	public List<MenuColumn> searchByName(String strMenuColumnName,boolean checkColumn){
		
		if(checkColumn){
			// 栏目关联判断
			String strRelatedColumns = getRelatedColumns();
			if(!"".equals(strRelatedColumns)){
					return columnMapper.searchCheckedByName(strMenuColumnName,strRelatedColumns.split(","));
			} else {
				return null;
			}
		} else {
			return columnMapper.searchByName(strMenuColumnName);
		}
	}
	
	/**
	 * 通过栏目名字查找栏目
	 * 
	 * @param String
	 * 			strMenuColumnName 栏目名字
	 */
	@Transactional(readOnly = true)
	public List<MenuColumn> searchByNameAndPid(String strMenuColumnName,Integer intParentId,boolean checkColumn){
		
		if(checkColumn){
			// 栏目关联判断
			String strRelatedColumns = getRelatedColumns();
			if(!"".equals(strRelatedColumns)){
					//return columnMapper.searchCheckedByNameAndPid(strMenuColumnName,intParentId,strRelatedColumns.split(","));
				   Query que = finder.from("column").where("name",strMenuColumnName).where(" id in ("+strRelatedColumns+")");
				   if(intParentId==null){
					   que.where(" parent_id is NULL ");
				   } else {
					   que.where("parent_id",intParentId);
				   }
				   return que.all(MenuColumn.class);
			} else {
				return null;
			}
		} else {
			   Query que = finder.from("column").where("name",strMenuColumnName);
			   if(intParentId==null){
				   que.where(" parent_id is NULL ");
			   } else {
				   que.where("parent_id",intParentId);
			   }
			   return que.all(MenuColumn.class);
		}
	}
	
	/**
	 * 增加栏目
	 * 
	 * @param menuColumn 
	 * 		
	 */
	@Transactional(rollbackFor = Exception.class)
	public void addColumn(MenuColumn menuColumn){
		
		columnMapper.insertColumn(menuColumn);
		// sortWay
		if(menuColumn.getParentId()==null && menuColumn.getSortWay()==null){
			try{
				menuColumn.setSortWay(menuColumn.getId()*100);
				columnMapper.updateColumn(menuColumn);
			} catch(Exception e){
				e.printStackTrace();
			}
		}
		// 将此栏目的权限加入到关联栏目中
		ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes();
		HttpServletRequest request = attrs.getRequest();
		Operator operator = (Operator)request.getSession().getAttribute("currentOperator");

		String strRelatedColumns = operator.getOperatorGroups().getRelatedColumns();
		if(strRelatedColumns.length() > 0){
			if(strRelatedColumns.charAt(strRelatedColumns.length()-1)!=','){
				strRelatedColumns += ",";
			}
			
			
			strRelatedColumns += menuColumn.getId() + ",";
			Long lngGroupID = operator.getOperatorGroups().getGroupId();
			operator.getOperatorGroups().setRelatedColumns(strRelatedColumns);
			if( lngGroupID != null){
				changeRelatedColumns(request,operator);
			} else {
				throw new RuntimeException("角色ID不能为null");
			}
		}
	}
	
	
	
	/**
	 * 更新栏目
	 * 
	 * @param menuColumn
	 */
	@Transactional(rollbackFor = Exception.class)
	public void updateColumn(MenuColumn menuColumn){
		columnMapper.updateColumn(menuColumn);
	}
	
	/**
	 * 删除栏目
	 * 
	 * @param menuColumn
	 */
	@Transactional(rollbackFor = Exception.class)
	public void deleteColumns(String strColumnIDs){
System.out.println("===================================");
		System.out.println("strColumnIDs:s" + strColumnIDs +"s");
		System.out.println("===================================");
		columnMapper.deleteColumns(strColumnIDs.split(","));
		
	}
	
	/**
	 * 修改关联栏目
	 */
	public void changeRelatedColumns(HttpServletRequest request,Operator operator){

		// 更新关联栏目列表
		columnMapper.updateRelatedColumns(operator.getOperatorGroups().getGroupId(),
				operator.getOperatorGroups().getRelatedColumns());
		// 修改session中的信息
		request.getSession().setAttribute("currentOperator",operator);
	}
	
	/**
	 * 搜索
	 * 
	 * @param searchText 搜索内容
	 */
	public List<MenuColumn> searchColumn(String searchText,boolean blnCheck){
		
		if(blnCheck) {
			String strRelatedColumns = getRelatedColumns();
			if(strRelatedColumns!=""){
				return finder.from("column").where(" name like '%"+searchText + "%'")
				.where(" id in ("+strRelatedColumns+")").orderBy("sort_way,id").all(MenuColumn.class);
			} else {
				return null;
			}
		} else {
			return finder.from("column").where(" name like '%"+searchText + "%'").orderBy("sort_way,id").all(MenuColumn.class);
		}
	}

	/**
	 * 得到关联栏目
	 */
	private String getRelatedColumns(){
		
		ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes();
		HttpServletRequest request = attrs.getRequest();
		Operator operator = (Operator)request.getSession().getAttribute("currentOperator");
		if(operator!=null){
			OperatorGroups operatorGroups = operator.getOperatorGroups();
			// 所属角色
			if(operatorGroups!=null){
				String strRelatedColumns = operatorGroups.getRelatedColumns();
				//就是，
				if(strRelatedColumns!=null&&!"".equals(strRelatedColumns.trim())){
					// 去掉空字符串
					strRelatedColumns = strRelatedColumns.trim();
					// 去掉前面的,
					if(strRelatedColumns.length()>0) {
						char charFst = strRelatedColumns.charAt(0);
						if(charFst==','){
							strRelatedColumns = strRelatedColumns.substring(1, strRelatedColumns.length()).trim();
						}
						// 去掉后面的,
						if(strRelatedColumns.length()>0){
							char charLast = strRelatedColumns.charAt(strRelatedColumns.length()-1);
							if(charLast==','){
								strRelatedColumns = strRelatedColumns.substring(0, strRelatedColumns.length()-1).trim();
							}
							if(strRelatedColumns.length()>0){
								return strRelatedColumns;
							} else {
								return "";
							}
						} else {
							return "";
						}
					} else {
						return "";
					}
				} else {
					return "";
				}
			} else {
				return "";
			}
		} else {
			return "";
		}
	}
}
